//
//  CAUserInfo.m
//  cascade-realm
//
//  Created by QSC on 20/5/24.
//

#import "CAUserInfo.h"
#import "CALoginController.h"
@implementation CAUserInfo
static CAUserInfo *_info = nil;
+ (instancetype)shared {
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _info = [[CAUserInfo alloc]init];
    });
    return _info;
}

- (NSMutableArray *)followIds{
    if (_followIds == nil) {
        _followIds = [NSMutableArray array];
    }
    return  _followIds;
}


- (NSString *)showID{
    return  self.userId;

}


+(void)refreshUserInfo:(void(^)(void))success{

    NSString * userId = [[NSUserDefaults standardUserDefaults] objectForKey:@"userId"];
    if (userId == nil) {
        return;
    }
    [Http getWithURL:getUserInfo params:@{@"userId":userId } success:^(id  _Nonnull responseObject) {
        NSDictionary * data = responseObject[@"data"];
        
        NSMutableDictionary * data2 = [NSMutableDictionary dictionaryWithDictionary:data];
        data2[@"avatar"] = data2[@"avatarUrl"];
        
        
        CAUserInfo *userinfo = [CAUserInfo mj_objectWithKeyValues:data2];
       
        [CAUserInfo shared].userInfo = userinfo;
        
        
        //保存一下自己的信息
        [DBTool saveData:data2 tag:userId];
        
        !success ? : success();
    } failure:^(id  _Nonnull errObject) {
        
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"token"];
        [UIApplication sharedApplication].keyWindow.rootViewController = [CALoginController new];
    }];
}

@end
